using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace CoolStuff
{   
	public partial class TestForm : Form
	{
		
		public TestForm()
		{
			InitializeComponent();
			
	     }

		PicDoc picDoc = null;
		BrowserForm browserForm=null;
        Util util = new Util();

        private void DoTwitterSearch(string username)
        {
            //get tweets for a user
            string html = util.DoGet("https://twitter.com/" + username, "");

            Hashtable tweets = new Hashtable();

            //while (html.Contains("<div class=\"Grid\" data-component-term=\"tweet\" role=\"presentation\">"))
            while (html.Contains("<div class=\"stream-item-header\">"))
            {
                Hashtable mentions = new Hashtable();
                Hashtable hashtags = new Hashtable();

                string timestamp = util.ParseValue(html, "tweet-timestamp js-permalink js-nav js-tooltip\" title=\"", "\"");
                //string[] temp = util.ParseValue(timestamp, "title=\"", "\"").Split('-');
                string[] temp = timestamp.Split('-');
                timestamp = temp[1] + " " + temp[0];
                DateTime created = DateTime.Parse(timestamp);
                string id = util.ParseValue(html, "data-item-id=\"", "\"");

                string text = util.ParseValue(html, "data-aria-label-part=\"0\">", "</p>");

                string mentionsHtml = text;

                while (mentionsHtml.Contains("<s>@</s>"))
                {
                    string mention = util.ParseValue(mentionsHtml, "<b>", "</b>");

                    try
                    {
                        mentions.Add(mention, mention);
                    }
                    catch (Exception ex)
                    { }

                    mentionsHtml = mentionsHtml.Substring(mentionsHtml.IndexOf("<s>@</s>") + 5);
                }

                mentions.Add("sdkeene", "sdkeene");
                mentions.Add("sharepoint", "sharepoint");
                mentions.Add("shrpntknight", "shrpntknight");

                string hashText = text;

                while (hashText.Contains("<s>#</s>"))
                {
                    string mention = util.ParseValue(hashText, "<b>", "</b>");

                    try
                    {
                        hashtags.Add(mention, mention);
                    }
                    catch (Exception ex)
                    { }

                    hashText = hashText.Substring(hashText.IndexOf("<s>#</s>") + 5);
                }

                TweetData td = new TweetData();
                td.TwitterId = username;
                td.Id = id;
                td.Text = text;
                td.Created = created;
                td.Mentions = mentions;
                td.Hashtags = hashtags;

                try
                {
                    tweets.Add(id, td);
                }
                catch (Exception ex)
                {
                }

                html = html.Substring(html.IndexOf("<div class=\"stream-item-header\">") + 5);
            }

            foreach (TweetData td in tweets.Values)
            {
                System.Threading.Thread thdBrowser = new System.Threading.Thread(new ParameterizedThreadStart(GetTweet));
                thdBrowser.SetApartmentState(System.Threading.ApartmentState.STA);
                thdBrowser.Start((object)td);

                Application.DoEvents();
                return;

                /*
                //get snapshot of a tweet
                string url = "https://twitter.com/givenscj/status/" + id;
                picDoc = new PicDoc();
                picDoc.refreshPicture += new RefreshPictureDelegate(picDoc_refreshPicture);
                picDoc.Url = url;
                picDoc.PicSize = (PSize)this.cbPicSize.SelectedItem;
                browserForm.CurDoc = picDoc;
                browserForm.getImageFromUrl(picDoc.Url, id);                
                */

                //generate word doc of tweet...

                //upload to O365...

                //set site to reindex...

                //browse to delve...			
            }
        }
		
		private void btnGo_Click(object sender, EventArgs e)
		{            
            

        }

        

        private void GetTweet(object obj)
        {
            TweetData td = (TweetData)obj;

            //get snapshot of a tweet
            string url = "https://twitter.com/givenscj/status/" + td.Id;
            BrowserForm bf = new BrowserForm();            
            bf.getImageFromUrl(url, td);
            bf.Dispose();
        }

        /// <summary>
        /// Here I paint a border to picture and  display it
        /// </summary>
        /// <param name="im"></param>
        private void picDoc_refreshPicture(Image im) {

			Graphics g = Graphics.FromImage(im);
			g.DrawRectangle(Pens.Blue,new Rectangle(0,0,im.Width-1,im.Height-1));			
		
		
		}
		/// <summary>
		/// Here I populate combo box with PSize objects representing sizes available for thumbnails.
		/// Invisible BrowserForm is created  here as well.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void TestForm_Load(object sender, EventArgs e)
		{
			browserForm = new BrowserForm();

		}

        private void btnGo_Click_1(object sender, EventArgs e)
        {
            //DoTwitterSearch("givenscj");
            DoTwitterSearch("shrpntknight");
            //DoTwitterSearch("alcip");
            //DoTwitterSearch("mikaelsvenson");
            //DoTwitterSearch("wictor");
            //DoTwitterSearch("williambaer");
        }
    }
}